﻿
$(function (){
    $('#SearchInput').on({
        'focusin' : search,
        'keyup' : search
    });
    $('#ACList').on('click', '.AC-item', function (){
        var productId = $(this).data('id');
        $('#AutoComplete').hide();
        window.location.href = '/ProductForm?productId=' + productId;
    });

    $('#AutoComplete').on('mouseleave', function (){
        $(this).hide();
    });
})

function search(){
    var info = $(this).val().trim();
    if(info === null || info === '' || info.length === 0){
        $('#AutoComplete').hide();
        return;
    }
    $.ajax({
        type    : 'GET',
        url     : 'SearchAutoComplete?searchInfo=' + info,
        success : function (data){
            var productListHTML = '';
            for(var i = 0;i < data.length;i++){
                productListHTML += '<li class=\"AC-item\" data-id=\"' + data[i].productId + '\">' + data[i].productId + ':' + data[i].breed + '</li>';
            }
            $('#ACList').html(productListHTML);
            $('#AutoComplete').show();
        },
        error   : function (errorMsg){
            console.log(errorMsg);
        }
    });
}